package cn.edu.xmist.supermarket.bjb.controller;

import cn.edu.xmist.supermarket.bjb.config.Vars;
import cn.edu.xmist.supermarket.bjb.entity.UpdateUserPO;
import cn.edu.xmist.supermarket.bjb.service.UpdatesService;
import cn.edu.xmist.supermarket.bjb.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.Update;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import javax.swing.*;
import java.util.List;
@RestController
@RequestMapping("/updateuser")
public class UpdateuserController {
    @Autowired
    private UpdatesService updateuserService;
    @Autowired private HttpSession httpSession;

    @RequestMapping("/updateHtml")
    public String login() {
        return "update";
    }

    @ResponseBody
    @RequestMapping("/update")
    public String update(UpdateUserPO user) {
        QueryWrapper<UpdateUserPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", user.getUsername());
        List<UpdateUserPO> list = updateuserService.list(queryWrapper);
        if (list.size() > 0) {
            if (user.getPassword().equals(list.get(0).getPassword())) {
                UpdateWrapper updateWrapper=new UpdateWrapper();
                updateWrapper.eq("username",user.getUsername());
                updateWrapper.set("password",user.getUpdetapassword());


                return Vars.SUCCESS;
            }
        }
        return Vars.FAILURE;
    }

    @ResponseBody
    @RequestMapping("/logout")
    public String logout() {
        httpSession.invalidate();
        return Vars.SUCCESS;
    }
}
